/**
 * 性别过滤器
 * @param {*} value 性别枚举值
 * @param {*} data 性别数据
 */
const  sexFilter = (value, data) => {
  let text = '-'
  data.map(item => {
    value === item.id && (text = item.value)
  })
  return text
}

/**
 * 学历过滤器
 * @param {*} value 学历枚举值
 * @param {*} data 学历数据
 */
const educationFilter = (value, data) => {
  let text = '-'
  data.map(item => {
    value === item.id && (text = item.value)
  })
  return text
}

/**
 * 求职状态过滤器
 * @param {*} value 求职状态枚举值
 */
const jobStateFilter = (value) => {
  if (value === 'work') {
    return '已工作'
  } else if (value === 'planing') {
    return '计划跳槽'
  } else if (value === 'hunting') {
    return '已离职，随时到岗'
  } else if (value === 'holiday') {
    return '休假'
  } else {
    return '已离职，随时到岗'
  }
}

/**
 * 求职岗位过滤器
 * @param {*} value 
 * @param {*} data 
 */
const postNameFilter = (value, data) => {
  let text = '-'
  data.map(item => {
    item.id === value && (text = item.name)
    item.children.map(childItem => {
      childItem.id === value && (text = item.name)
    })
  })
  return text
}

// 薪资要求
const salaryFilter = (min, max) => {
  min = (min === 0 || min) && String(min)
  max = (max === 0 || max) && String(max)
  if (!min && max) {
    return `期望薪资${max}`
  } else if (min && !max) {
    return `不低于${min}`
  } else if (min && max) {
    return `${min}-${max}`
  } else {
    return '-'
  }
}

// 工作经验
const workExperienceFilter = (value) => {
  if (!value || value.length === 0) {
    return '-'
  } else {
    let date = new Date;
    let nowYear = date.getFullYear() // 当前年
    let nowMonth = date.getMonth() + 1 // 当前月
    let year = +value.split('-')[0] // 工作开始年
    let month = +value.split('-')[1] // 工作开始月

    if (nowMonth === month) {
      return `${nowYear - year}年`
    } else if (nowMonth > month) {
      return `${(nowYear - year) + 1}年内`
    } else if (nowMonth < month) {
      return `${(nowYear - year) + 1}年内`
    }
  }
}

// 求职城市

const addressFilter = (value, data) => {
  let address = value.split(',')
  let text
  data.map(item => {
    if (item.cityCode === +address[1]) {
      text = item.location
    }
  })
  return text ||  '-'
}

// 增加更新时间字段的format
const formatUpdateTime = (value) => {
  return value == null ? '-' : value.substring(0,10);
}

export default {
  sexFilter,
  educationFilter,
  jobStateFilter,
  postNameFilter,
  salaryFilter,
  workExperienceFilter,
  addressFilter,
  formatUpdateTime
}